<!DOCTYPE html>

<script src="../../resources/js-test.js"></script>

<script>
description('Tests getting and assigning values to document.body');

onload = function() {
    frame = document.createElement('iframe');
    document.body.appendChild(frame);

    shouldThrow('frame.contentDocument.body = document.createElement("div")', '"HierarchyRequestError: Failed to set the \'body\' property on \'Document\': The new body element is of type \'DIV\'. It must be either a \'BODY\' or \'FRAMESET\' element."');
    shouldNotThrow('frame.contentDocument.body = document.createElement("frameset")');
    shouldBe('frame.contentDocument.documentElement.childNodes.length', '2');
    shouldNotThrow('frame.contentDocument.body = document.createElement("body")');
    shouldBe('frame.contentDocument.documentElement.childNodes.length', '2');

    observer = new MutationObserver(function(records) { });
    observer.observe(frame.contentDocument, { subtree: true, childList: true });
    // If the nodes are the same this should be a noop.
    frame.contentDocument.body = frame.contentDocument.body;
    shouldBe('observer.takeRecords().length', '0');

    // WebKit calls importNode() and appends a clone instead of the element you wanted.
    newBody = document.createElement("body");
    frame.contentDocument.body = newBody;
    shouldBe('frame.contentDocument.body', 'newBody');

    newBody = frame.contentDocument.createElement('body');
    frame.contentDocument.body = newBody;
    shouldBe("frame.contentDocument.body", "newBody")

    var html = frame.contentDocument.documentElement;
    html.appendChild(document.createElement('body'));
    html.appendChild(document.createElement('frameset'));
    shouldBeEqualToString('frame.contentDocument.body.tagName', 'BODY');
};
</script>

